// Decompiled by DJ v3.9.9.91 Copyright 2005 Atanas Neshkov  Date: 22/03/2010 14:55:25
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   EnviaEmail.java
package br.projecao.laboratorio.util;

import com.sun.net.ssl.internal.ssl.Provider;
import java.io.File;
import java.security.Security;
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

// Referenced classes of package email:
//            Email
public class EnviaEmail extends Thread {

  public EnviaEmail(Email email) {
    this.email = email;
  }

  public void run() {
    Properties props = System.getProperties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "465");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.starttls.enable", "true");
    Security.addProvider(new Provider());
    Session sessao = Session.getDefaultInstance(props, new PopupAuthenticator());
    MimeMessage msg = new MimeMessage(sessao);
    Multipart mp = new MimeMultipart();
    try {
      msg.setSentDate(new Date());
      msg.setSubject(email.getAssunto());
      msg.setFrom(email.getRemetente());
      Map destino = email.getDestino();
      Iterator i$ = destino.keySet().iterator();
      do {
        if (!i$.hasNext()) {
          break;
        }
        char key = ((Character) i$.next()).charValue();
        if (key == 'C') {
          msg.setRecipients(javax.mail.Message.RecipientType.CC, (javax.mail.internet.InternetAddress[]) (javax.mail.internet.InternetAddress[]) destino.get(Character.valueOf(key)));
        } else if (key == 'O') {
          msg.setRecipients(javax.mail.Message.RecipientType.BCC, (javax.mail.internet.InternetAddress[]) (javax.mail.internet.InternetAddress[]) destino.get(Character.valueOf(key)));
        } else if (key == 'P') {
          msg.setRecipients(javax.mail.Message.RecipientType.TO, (javax.mail.internet.InternetAddress[]) (javax.mail.internet.InternetAddress[]) destino.get(Character.valueOf(key)));
        }
      } while (true);
      MimeBodyPart texto = new MimeBodyPart();
      if (!email.getIsHtml()) {
        texto.setText(email.getTexto());
      } else {
        texto.setContent(email.getTexto(), "text/html");
      }
      mp.addBodyPart(texto);
      if (email.getAnexo() != null) {
        File arquivos[] = email.getAnexo();
        for (int i = 0; i < arquivos.length; i++) {
          MimeBodyPart anexo = new MimeBodyPart();
          FileDataSource fds = new FileDataSource(arquivos[i].getAbsoluteFile());
          anexo.setDataHandler(new DataHandler(fds));
          anexo.setFileName(fds.getName());
          mp.addBodyPart(anexo);
        }

      }
      msg.setContent(mp);
      Transport transport = sessao.getTransport("smtp");
      transport.connect(email.getHost(), email.getUsuario(), email.getSenha());
      Transport.send(msg);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  class PopupAuthenticator extends Authenticator {

    String username;
    String password;

    public PopupAuthenticator() {
      this.username = email.getUsuario();
      this.password = email.getSenha();
    }

    @Override
    public PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(username, password);
    }
  }
  private Email email;
}
